/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxSpecialUtil
 * @Package com.rx.core.util
 * @Description: rx 特别工具
 * @author: 陈锦韬
 * @date: 2021\6\15 0015
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.util;

import com.rx.core.enm.RESULT_CODE;
import com.rx.core.handler.RxHandler;
import com.rx.core.handler.RxHandlerAfter;
import com.rx.core.handler.RxHandlerPrev;
import com.rx.core.iface.*;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: Administrator
 * @Description: rx 特别工具
 * @date: 2021\6\15 0015
 */
public class RxSpecialUtil {

    public static Object AnnoForHandler(Class anno, ForPrevHandler prevHandler, ForOverHandler overHandler, ForAfterHandler afterHandler) throws Throwable {
        if (anno == RxHandlerPrev.class){
            return prevHandler.handle();
        }else if (anno == RxHandlerAfter.class){
            return afterHandler.handle();
        }else if (anno == RxHandler.class){
            return overHandler.handle();
        }
        RxUtil.CheckUtil.throwOut(RESULT_CODE.FAIL);
        return null;
    }
    public static Object getByLock(boolean casLock, ReentrantLock lock, ReturnMode handler){
        if (casLock){
            try{
                lock.lock();
                return handler.handle();
            }finally {
                lock.unlock();
            }
        }else {
            return handler.handle();
        }
    }
    public static void setByLock(ReentrantLock lock, InputMode inputMode,NotReturnMode handler){
        try{
            inputMode.handle(true);
            lock.lock();
            handler.handle();
        }finally {
            lock.unlock();
            inputMode.handle(false);
        }
    }
}
